package com.ultrasoft.farmasoft.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Item;
import com.ultrasoft.farmasoft.domain.Pharmacy;
import com.ultrasoft.farmasoft.domain.Pharmacystock;
import com.ultrasoft.farmasoft.domain.Stock;
import com.ultrasoft.farmasoft.domain.Transfer;
import com.ultrasoft.farmasoft.domain.Userpharmacy;
import com.ultrasoft.farmasoft.repository.ItemDao;
import com.ultrasoft.farmasoft.repository.PharmacyDao;
import com.ultrasoft.farmasoft.repository.PharmacyStockDao;
import com.ultrasoft.farmasoft.repository.StockDao;
import com.ultrasoft.farmasoft.repository.UserPharmacyDao;

@Service
public class StockServiceImpl implements StockService {
	private StockDao stockDao;
	private PharmacyDao pharmacyDao;
	private PharmacyStockDao pharmacyStockDao;
	private UserPharmacyDao userPharmacyDao;
	private ItemDao itemDao;
	
	@Autowired
	public StockServiceImpl(ItemDao itemDao,UserPharmacyDao userPharmacyDao,PharmacyDao pharmacyDao,StockDao stockDao,PharmacyStockDao pharmacyStockDao) {
		this.pharmacyStockDao 	= pharmacyStockDao;
		this.stockDao 			= stockDao;
		this.pharmacyDao 		= pharmacyDao;
		this.userPharmacyDao 	= userPharmacyDao; 
		this.itemDao 			= itemDao;
	}
	@Transactional(readOnly = true)  
	public Userpharmacy searchUserPharmacyByPharmacyIdByUserId(Integer pharmacyId,Integer userId){
		return userPharmacyDao.findByPharmacyIdByUserId(pharmacyId, userId);
	}
	@Transactional(readOnly = true)   
	public List<Userpharmacy> getListUserPharmacy(boolean active){
		return userPharmacyDao.getUserPharmacyList(active);
	}
	@Transactional(readOnly = true)   
	public List<Userpharmacy> getListUserPharmacy(Integer pharmacyId,boolean active){
		return userPharmacyDao.getListUserPharmacy(pharmacyId, active);
	}
	
	
	@Transactional(readOnly = true)   
	public List<Pharmacy> getListPharmacy() {
		return pharmacyDao.getListPharmacy();
	}
	@Transactional(readOnly = true)  
	public List<Pharmacy> getListPharmacyByPhapharmacyIdNull(boolean active){
		List<Pharmacy> temp = new ArrayList<Pharmacy>();
		List<Pharmacy> listPharmacy = pharmacyDao.getListPharmacy(active);
		for (Pharmacy pharmacy : listPharmacy) {
			if(pharmacy.getPhapharmacyId() == null)
				temp.add(pharmacy);
		}
		return temp;
	}
	@Transactional(readOnly = true)   
	public List<Pharmacy> getListPharmacy(boolean active) {
		return pharmacyDao.getListPharmacy(active);
	}
	@Transactional(readOnly = true)   
	public List<Pharmacy> getListPharmacy(Integer regionId) {
		return pharmacyDao.getListPharmacy(regionId);
	}
	@Transactional(readOnly = true)
	public List<Pharmacy> getListPharmacy(Integer regionId,boolean active) {
		return pharmacyDao.getListPharmacy(regionId, active);
	}
	@Transactional(readOnly = true)   
	public Pharmacy searchPharmacyByPharmacyId(Integer pharmacyId) {
		return pharmacyDao.findByPharmacyId(pharmacyId);
	}
	@Transactional
	public void updatePharmacy(Pharmacy pharmacy) {
		pharmacyDao.update(pharmacy);
	}
	@Transactional
	public void removePharmacy(Pharmacy pharmacy) {
		pharmacyDao.remove(pharmacy);
	}
	@Transactional
	public void addPharmacy(Pharmacy pharmacy) {
		pharmacyDao.add(pharmacy);
	}
	
	
	@Transactional(readOnly = true)   
	public List<Stock> getListStock() {
		return stockDao.getListStock();
	}
	@Transactional(readOnly = true)   
	public List<Stock> getListStockByPharmacyIdByUserId(Integer pharmacyId, Integer userId) {
		return stockDao.getListStockByPharmacyIdByUserId(pharmacyId, userId);
	}
	@Transactional(readOnly = true)   
	public List<Stock> getListStockByPharmacyId(Integer pharmacyId) {
		return stockDao.getListStockByPharmacyId(pharmacyId);
	}
	
	// por el momento pendiente-------
	/*
	
	@Transactional(readOnly = true)   							
	public List<Stock> getListStockByPharmacyIdIntoPharmacyStock(Integer pharmacyId) {
		List<Stock> res 		= new ArrayList<Stock>();
		List<Stock> stockList 	= stockDao.getListStock();
		List<Pharmacystock> listPharmacyStock = pharmacyStockDao.getListPharmacyStockByPharmacyId(pharmacyId);
		for (Pharmacystock pharmacystock : listPharmacyStock) {
			for (Stock stock : stockList) {
				if(pharmacystock.getStock().getStockId() == stock.getStockId()){
					stock.setItemCollection(itemDao.getListItemByStockId(stock.getStockId()));
					res.add(stock);
				}
			}
		}
		for (Stock stock : res) {
			System.out.println(stock.getName());
			List<Item> items = (List<Item>)stock.getItemCollection();
			for (Item item : items) {
				System.out.println("entro "+item.getState());
			}
		}
		return res;
	}
	
	*/
	/*
	@Transactional(readOnly = true)   							
	public List<Stock> getListStockByPharmacyIdIntoPharmacyStock2(Integer pharmacyId) {
		List<Stock> res 		= new ArrayList<Stock>();
		List<Stock> stockList 	= stockDao.getListStock();
		List<Pharmacystock> pharmacyStockList = pharmacyStockDao.getListPharmacyStockByPharmacyId(pharmacyId);
		for (Pharmacystock pharmacystock : pharmacyStockList) {
			for (Stock stock : stockList) {
				if(pharmacystock.getStock().getStockId() == stock.getStockId())
					res.add(stock);
			}
		}
		for (Stock stock : res) {
			List<Item> items = itemDao.getListItemByStockId(stock.getStockId());
			stock.setItemCollection(items);
		}
		return res;
	}*/
	@Transactional(readOnly = true)   
	public List<Stock> getListStockByUserId(Integer userId) {
		return stockDao.getListStockByUserId(userId);
	}
	@Transactional(readOnly = true)   
	public Stock searchStockByStockId(Integer stockId) {
		return stockDao.findByStockId(stockId);
	}
	@Transactional
	public void updateStock(Stock stock) {
		stockDao.update(stock);
	}
	@Transactional
	public void removeStock(Stock stock) {
		stockDao.remove(stock);
	}
	@Transactional
	public void addStock(Stock stock) {
		stockDao.add(stock);
	}

	
	@Transactional(readOnly = true)   
	public List<Pharmacystock> getListPharmacyStock() {
		return pharmacyStockDao.getListPharmacyStock();
	}
	@Transactional(readOnly = true)   
	public List<Pharmacystock> getListPharmacyStockByPharmacyId(Integer pharmacyId) {
		List<Pharmacystock> res = new ArrayList<Pharmacystock>();
		List<Pharmacystock> list = pharmacyStockDao.getListPharmacyStockByPharmacyId(pharmacyId);
		for (Pharmacystock pharmacystock : list) {
			pharmacystock.getStock().setItemCollection(itemDao.getListItemByStockId(pharmacystock.getPharmacystockPK().getStockId()));
			/*
			pharmacystock.getPharmacy().setPharmacyCollection(pharmacyDao.getListPharmacyByPharmacyIdByActive(pharmacyId,true));
			List<Transfer> t = new ArrayList<Transfer>();
			pharmacystock.getStock().setTransferCollection(t);
			pharmacystock.getStock().setTransferCollection1(t);
			*/
			res.add(pharmacystock);
		}
		return res;
	}
	@Transactional(readOnly = true)   
	public List<Pharmacystock> getListPharmacyStockByPharmacyIdByProviderId(Integer pharmacyId,Integer providerId) {
		List<Pharmacystock> res = new ArrayList<Pharmacystock>();
		List<Pharmacystock> list = pharmacyStockDao.getListPharmacyStockByPharmacyId(pharmacyId);
		for (Pharmacystock pharmacystock : list) {
			pharmacystock.getStock().setItemCollection(itemDao.getListItemByStockIdByProviderId(pharmacystock.getPharmacystockPK().getStockId(),providerId));
			res.add(pharmacystock);
		}
		return res;
	}
	@Transactional(readOnly = true)   
	public List<Pharmacystock> getListPharmacyStockByStockId(Integer stockId) {
		return pharmacyStockDao.getListPharmacyStockByStockId(stockId);
	}
	@Transactional(readOnly = true)   
	public Pharmacystock searchPharmacyStockByPharmacyIdByStockId(Integer pharmacyId, Integer stockId) {
		return pharmacyStockDao.findByPharmacyIdByStockId(pharmacyId, stockId);
	}
	@Transactional
	public void updatePharmacyStock(Pharmacystock pharmacyStock) {
		pharmacyStockDao.update(pharmacyStock);
	}
	@Transactional
	public void removePharmacyStock(Pharmacystock pharmacyStock) {
		pharmacyStockDao.remove(pharmacyStock);
	}
	@Transactional
	public void addPharmacyStock(Pharmacystock pharmacyStock) {
		pharmacyStockDao.add(pharmacyStock);
	}
}
